﻿#pragma once

#include <QTabWidget>
#include <Global.h>
#include <WindowInterface.h>
#include "XMLCoreExport.h"

namespace Domy {

class XMLCoreEditor;

/**
 * @brief The XMLCoreWindow class
 * @note  XML核心窗口
 */
class XMLCORE_EXPORT XMLCoreWindow : public QTabWidget, public WindowInterface
{
    Q_OBJECT

public:
    explicit XMLCoreWindow(ActionEnableCtrl ctrl, QWidget *parent = Q_NULLPTR);
    ~XMLCoreWindow();

    /**
     * @brief newDocument
     * @note  新建文档
     */
    void newDocument();

    /**
     * @brief openFile
     * @note  打开
     */
    bool openDocument(const QString &file_path);

    /**
     * @brief closeCurrentDocument
     * @note  关闭当前文档
     */
    bool closeCurrentDocument();

    /**
     * @brief closeAllDocuments
     * @note  关闭所有文档
     */
    bool closeAllDocuments();

    /**
     * @brief  saveCurrentDocument
     * @note   保存文档
     */
    FileSavedReturnCode saveCurrentDocument();

    /**
     * @brief saveAsCurrentDocument
     * @note  当前文档另存为
     * @param file_path 文件路径
     */
    FileSavedReturnCode saveAsCurrentDocument(const QString &file_path);

    /**
     * @brief saveAllDocument
     * @note  保存所有文档
     */
    bool saveAllDocuments();

    /**
     * @brief doAction
     * @note  触发动作
     */
    virtual void doAction(ActionKey key) override;

    /**
     * @brief updateSettings
     * @note  更新设置
     */
    void updateSettings();

signals:
    /**
     * @brief newDocumentFilePathChanged
     * @note  新建文档文件路径改变信号
     * @param file_path 文件路径
     */
    void signalNewDocumentFilePathChanged(const QString &file_path);

private slots:
    void slotOnCurrentIndexChanged(int index);
    void slotOnTabCloseRequested(int index);
    void slotOnContentTextChanged();
    void slotOnContentModificationChanged();
    void slotOnFilePathChanged();
    void slotOnSyntaxCheckTimeout();
    void slotOnEditorUndoAvailable(bool available);
    void slotOnEditorRedoAvailable(bool available);
    void slotOnTabBarMenuRequested(const QPoint &pos);

private:
    bool newDocument(const QString &file_path);
    FileSavedReturnCode saveDocument(XMLCoreEditor *editor, QString * const error = Q_NULLPTR);
    bool closeDocument(int index);
    void updateDocumentChangeState(int index);
    void updateCurrentDocumentChangeState();
    void updateAllDocumentsChangeState();
    void checkSyntax();
    void setDocumentActionEnable(bool enable);

private:
    QTimer     *m_syntax_timer;
    QStringList m_files_path;
};

} // namespace Domy
